$(function () {

    var init = function (params) {

        var postUrl = '/commodityType/doInsert';

        var $form = $('form').bootstrapValidator({
            fields: {
                title: {
                    validators: {
                        notEmpty: {
                            message: '类型名称不能为空'
                        }
                    }
                }
            }
        });

        //判断是否是 编辑页面

        if (params) {
            postUrl = '/commodityType/doUpdate';
            $form.find('.form-control[name="title"]').val(params.title)
            $form.find('.form-control[name="sort"]').val(params.sort);
        }

        $("#submit").on("click", function () {
            var bootstrapValidator = $form.data('bootstrapValidator');

            bootstrapValidator.validate();

            if (bootstrapValidator.isValid()) {

                var param = {
                    title: $form.find('.form-control[name="title"]').val(),
                    sort: $form.find('.form-control[name="sort"]').val()
                };


                if (params) {
                    param.id = params.id
                }

                $.restPost(postUrl, param).done(function (msg) {

                    $.rootSuccessDialog(msg, 1000, function () {
                        location.hash = '/product/managetype'
                    })

                }).fail(function (msg) {

                    $.rootErrorDialog(msg, 1000)
                });
            }

        })

    };

    if (/typeedit/.test(location.hash)) {
        $.restPost("/commodityType/doSelect", {
            id: $.getUrlLocation()
        }).done(function (mess, data) {
            init(data)
        })

    } else {
        init(false)

    }

})